<template>
	<view>
		<view class="recovered_box">
			<view class="empty_box" v-if="dataList.length == 0 && isLoading == false">
				<image src="@/static/index/emptyBox.png" mode=""></image>
				<text>什么都没有</text>
			</view>
			<dataItem :dataList="dataList" :disabled="false" :type="'onlyDel'"></dataItem>
			<!-- 内容提示 -->
			<view class="bottom" v-if="isLoadedAll">
				———— 我也是有底线的 ————
			</view>
		</view>
	</view>
</template>

<script>
	import dataItem from '@/components/dataItem/dataItem.vue'

	const app = getApp()

	export default {
		name: 'myGetBack',
		props: {
			dataItem
		},
		data() {
			return {
				// 当前删除信息的状态,固定为2
				del_index: 2,
				// 是否全部加载完成
				isLoadedAll: false,
				// 正在触底加载
				isLoading: false,
				// 数据总数
				total: 0,
				// 当前页数
				pageNum: 0,
				// 固定获取的数量
				numbers: 8,
				// 用户信息
				userInfo: {},
				// 数据列表
				dataList: [],
				// 数据数组
				dataArray: []
			}
		},
		methods: {
			// 初始化函数
			init() {
				this.isLoading = true
				uni.showLoading({
					title: '加载中...'
				})
				this.isLoadedAll = false
				this.pageNum = 0
				this.dataList = []
				this.dataArray = []
				this.userInfo = app.globalData.userInfo
				// 获取数据信息
				this.get_total().then(res => {
					console.log(res);
					this.dataArray = res.result.data[0].get_back
					this.total = res.result.data[0].get_back.length
					if (this.total != 0) {
						this.get_info()
					} else {
						uni.hideLoading()
						uni.stopPullDownRefresh()
						// 节流阀关闭
						this.isLoading = false
					}
				})
			},

			// 获取我的发布数据的总数
			get_total() {
				return new Promise(resolve => {
					wx.cloud.callFunction({
						name: 'get_total',
						data: {
							UID: this.userInfo.profile.student_ID
						}
					}).then(res => {
						resolve(res)
					})
				})
			},

			// 获取我的发布数据
			get_info() {
				wx.cloud.callFunction({
					name: 'get_info',
					data: {
						dataArray: this.dataArray,
						numbers: this.numbers,
						dataList_len: this.dataList.length,
						total: this.total
					}
				}).then(res => {
					console.log(res);
					uni.hideLoading()
					uni.stopPullDownRefresh()
					// 节流阀关闭
					this.isLoading = false
					this.dataList = [...this.dataList, ...res.result]
					this.pageNum++
					// 判断是否加载完成
					if (this.total != 0 && this.pageNum * this.numbers >= this.total) {
						return this.isLoadedAll = true
					}
				})
			}
		},
		onLoad() {
			this.init()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.init()
		},
		// 触底加载
		onReachBottom() {
			// 节流阀
			if (this.isLoading) {
				return 0
			}
			// 判断是否加载完成
			if (this.pageNum * this.numbers >= this.total) {
				return this.isLoadedAll = true
			}
			uni.showLoading({
				title: '加载中...'
			})
			// 节流阀开启
			this.isLoading = true
			this.get_info()
		}
	}
</script>

<style scoped lang="scss">
	.recovered_box {
		width: 100vw;
		display: flex;
		flex-direction: column;
		align-items: center;

		.empty_box {
			width: 100vw;
			height: 100vh;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			image {
				margin-top: -200rpx;
				width: 300rpx;
				height: 300rpx;
			}

			text {
				color: #B3B3B3;
				font-size: 32rpx;
			}
		}

		.bottom {
			margin: 30rpx 0 100rpx 0;
			font-size: 24rpx;
			color: #B3B3B3;
		}
	}
</style>
